package cn.demo.app.a.controller;

import cn.demo.app.a.domain.User;
import cn.demo.app.a.domain.vo.UserVo;
import cn.demo.app.a.service.UserService;
import cn.ifloat.brick.basic.common.accounts.Account;
import cn.ifloat.brick.basic.common.accounts.CurrentAccountInfoKit;
import cn.ifloat.brick.basic.common.accounts.drs.DataAuthMode;
import cn.ifloat.brick.basic.common.accounts.drs.DataRule;
import cn.ifloat.brick.basic.common.results.PageRequester;
import cn.ifloat.brick.basic.common.results.PageResult;
import cn.ifloat.brick.basic.common.results.R;
import cn.ifloat.brick.basic.common.toolkit.Maper;
import cn.ifloat.brick.basic.common.toolkit.StringUtils;
import cn.ifloat.brick.logger.annotations.OperationLog;
import cn.ifloat.brick.logger.annotations.PerformanceLog;
import cn.ifloat.brick.logger.common.PerformanceUnit;
import cn.ifloat.brick.logger.core.LogKit;
import cn.ifloat.brick.sprofile.cache.annonation.BCacheable;
import cn.ifloat.brick.sprofile.cache.annonation.BRepeatSubmit;
import cn.ifloat.brick.sprofile.common.init.config.SpringUtil;
import cn.ifloat.brick.sprofile.mybatis.tools.DataAuthSqlKit;
import cn.ifloat.brick.sprofile.web.common.BasicController;
import cn.ifloat.brick.sprofile.web.init.config.WebEnvironmentConfig;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 用户相关接口
 *
 * @author: float
 */
@Controller
@RequestMapping("user")
public class UserController extends BasicController {
    @Autowired
    private UserService userService;
    @Autowired
    private WebEnvironmentConfig webEnvironmentConfig;

    private UserService copyUserService;


    @PostConstruct
    public void init() {

        copyUserService = SpringUtil.getBean(UserService.class);
        logger.info("init method success");
    }

    /**
     * 用户列表页面

     current
     args
     ${now}
     result

     * @return 返回路径
     */
    @RequestMapping("list")
//    @PerformanceLog(unit = PerformanceUnit.MILLS)
//    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ${current.name}", application = "example", service = "user", condition = "")
    public String list(Model model, HttpServletRequest request) {
        logger.info("!!!!!!!!!!!!!!");
        logger.debug("!!!!!!!!!!!!!!");
        logger.error("!!!!!!!!!!!!!!");
        PageRequester<User> pr = new PageRequester<>();

//        List<DataRule> drs = new ArrayList<>();
//        DataRule r1 = new DataRule();
//        DataRule r2 = new DataRule();
//        DataRule r3 = new DataRule();
//        DataRule r4 = new DataRule();
//        DataRule r5 = new DataRule();
//        r5.setDkey("name");
//        r5.setAuthMode(DataAuthMode.SEL_CASD_JOB);
//        r5.setValue("[\"201\",\"202\"]");
//        r4.setDkey("name");
//        r4.setAuthMode(DataAuthMode.CUR_ORG);
//        r3.setDkey("name");
//        r3.setAuthMode(DataAuthMode.CUR_CASD_ORG);
//        r2.setDkey("id");
//        r2.setAuthMode(DataAuthMode.CUR_EMP);
//        r1.setDkey("id");
//        r1.setValue("[1,2,3]");
//        r1.setAuthMode(DataAuthMode.SEL_EMP);
//        drs.add(r1);
//        drs.add(r2);
//        drs.add(r3);
//        drs.add(r4);
//        drs.add(r5);
////        drs.add(r1);
////        drs.add(r1);
////        drs.add(r1);
//
//        Account account = new Account();
//        account.setId("1");
//        account.setName("float");
//        account.setSuperAdmin(false);
//        account.setOrgId("1");
//        account.setJobId("2");
//        account.setGroupId("3");
//        account.setDataRules(Maper.<String,List<DataRule>>builder().of("USER",drs).get());
//
//        CurrentAccountInfoKit.add(account);
//
//        String sqlCondition = DataAuthSqlKit.toAuthSqlCondition("a", "USER");


//        Map<String, Map> map = Maper.<String, Map>builder().
//                of("name01", Maper.<String, String>builder().of("value", "kv2").get()).
//                of("amount01", Maper.<String, String>builder().of("value", "kv3").get()).get();
//
//        Maper.<String,String>builder().of().of().of().get()
//
//        SqlRunner.db()


        User condition = new User();
        condition.setId(100000l);
        pr.setCondition(condition);
//        List<UserVo> userVos = userService.listVo();
        PageResult<User> page = userService.pager(pr);
        List<UserVo> lists = userService.listAll();

        LogKit.system().info("测试user数量:" + page.getSize());
        model.addAttribute("items", page.getResults());
//        int i = 1 / 0;
        return "user/list";
    }

    /**
     * 获取用户信息
     *
     * @return 返回用户信息的结果
     */
    @GetMapping("get")
    @ResponseBody
    @BCacheable(group = "USER_", key = "${args[0]}", isGroupMode = true,putOrDelete = false)
    public User get(Long id) {
        User bu = userService.builderUser(3, "xxx");
        Map<String, Object> bm = userService.builderMap();
        String bs = userService.builderStr();
        return bu;
    }
    @GetMapping("delete")
    @BCacheable(group = "USER_", key = "${args[0]}", putOrDelete = false,isGroupMode = true)
    public User delete(Long id) {
        return null;
    }

    @GetMapping("vrs")
    @ResponseBody
    @BRepeatSubmit
    public Object verifyRepeatSubmit(){
        return Collections.emptyMap();
    }

}
